package com.supermap.rootstock.admin.aritmetic;

import com.supermap.rootstock.common.core.utils.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author aqua
 * @description
 * @date 2019年09月20 16:51
 * @email 1301584033@qq.com
 */
@RestController
public class Upload {
	/**
	 * 文件上传
	 *
	 * @return
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public R handlFileUpload(@RequestParam("file") MultipartFile file) {
		Map<String,Object> map=new HashMap<>();
		String filePath=System.getProperty("user.dir")+"/file/";
		if (file.isEmpty()) {
			return 	new R<>();
		}

		// 读取文件内容并写入 指定目录中
		String fileName = file.getOriginalFilename();
		// String suffixName = fileName.substring(fileName.lastIndexOf("."));
		fileName = UUID.randomUUID() +  fileName.substring(fileName.lastIndexOf("."));

		File dest = new File(filePath + fileName);
		// 判断目录是否存在
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}

		try {
			file.transferTo(dest);
		} catch (IOException e) {

		}
		return 	new R<>(filePath + fileName);
	}


}
